//
//  PersonalDataViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/13.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class PersonalDataViewController: BaseViewController {

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        initBaseLayout()
        layoutPageSubviews()
        configUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response
    func logoutAction(sender: UIButton) {
        let userDefault = UserDefaults.standard
        userDefault.removeObject(forKey: "loginStatus")
        userDefault.synchronize()
        self.tabBarController?.selectedIndex = 0
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(tableView)
        self.view.addSubview(logoutBtn)
        self.view.backgroundColor = UIColor(hexString: "#E5E5E5")
    }
    
    func layoutPageSubviews() {
        tableView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(0)
            make.top.equalTo(self.view).offset(16)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(171)
        }
        
        logoutBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tableView.snp.bottom).offset(30)
            make.left.equalTo(self.view).offset(14)
            make.right.equalTo(self.view).offset(-14)
            make.height.equalTo(44)
        }
    }
    
    func configUI() {
        initNavItem(controller: self, imageName: "nav_back_n", highlitedImageName: "nav_back_h", selectedImageName: "", isLeft: true)
        self.title = "个人资料"
        self.automaticallyAdjustsScrollViewInsets = false
    }
    
    // MARK: - setters and getters
    var _tableView: UITableView!
    var tableView: UITableView {
        if _tableView == nil {
            _tableView = UITableView(frame: CGRect.zero, style: .plain)
            _tableView.backgroundColor = UIColor(hexString: "#E5E5E5")
            _tableView.showsVerticalScrollIndicator = false
            _tableView.isScrollEnabled = false
            _tableView.delegate = self
            _tableView.dataSource = self
            _tableView.register(PersonalDataCell.self, forCellReuseIdentifier: "PersonalDataCell")
        }
        return _tableView
    }
    
    var _logoutBtn: UIButton!
    var logoutBtn: UIButton {
        if _logoutBtn == nil {
            _logoutBtn = UIButton()
            _logoutBtn.backgroundColor = UIColor(hexString: "#252525")
            _logoutBtn.setTitle("退出登录", for: .normal)
            _logoutBtn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
            _logoutBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 16)
            _logoutBtn.addTarget(self, action: #selector(PersonalDataViewController.logoutAction(sender:)), for: .touchUpInside)
        }
        return _logoutBtn
    }

}

extension PersonalDataViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let string = "PersonalDataCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: string, for: indexPath) as! PersonalDataCell
        cell.index = indexPath.row
        cell.configUI()
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 80
        } else if indexPath.row == 1 || indexPath.row == 2 {
            return 43
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 1 {
            let alertController = CustomAlertViewController(title: "", message: "", preferredStyle: .alert)
            alertController.showAlerView(controller: self, cancelTitle: "取消", okTitle: "确定", textStr: "修改昵称")
        } else if indexPath.row == 2 {
            let alertController = CustomAlertViewController(title: "", message: "", preferredStyle: .alert)
            alertController.showAlerView(controller: self, cancelTitle: "取消", okTitle: "确定", textStr: "修改手机号")
        }
    }

    
}



